import { Component, Input } from '@angular/core';
import { FormLayout } from 'ng-devui';
import { UserService } from './../../../../@core/services/user/user.service';

@Component({
  selector: 'app-edit-review-flow-roles',
  templateUrl: './edit-review-flow-roles.component.html',
  styleUrls: ['./edit-review-flow-roles.component.scss']
})
export class EditReviewFlowRolesComponent {
  @Input() data:any
  disabled = false;
  sourceOption = []
  targetOption = [];
  layoutDirection: FormLayout = FormLayout.Horizontal;
  CreateApprovalGroup:any={
    approvalGroupId:'',
    name:'',
    users:[]
  }
  UpdateApprovalGroup:any={
    name:'',
    groups:[]
  }
  constructor( private UserService:UserService) {}
  ngOnInit(): void {
    this.CreateApprovalGroup=JSON.parse(JSON.stringify(this.data.rowItem))
    this.UserService.GetAllEmploy(1,9999).subscribe((data:any)=>{
    this.sourceOption = data.items
    .filter((item: { name: any; }) => !this.data.rowItem.users.map((item: { name: any; }) => item.name).includes(item.name))
    .map((item: { accountNumber: any; }) => {
      return { ...item, value: item.accountNumber, accountNumber: undefined };
    });
    })
    this.targetOption = this.CreateApprovalGroup.users|| []
    this.UpdateApprovalGroup.name = this.CreateApprovalGroup.name
    this.UpdateApprovalGroup.groups =this.CreateApprovalGroup.users.map((item: { accountNumber: any; }) => item.accountNumber)
  }
  nameChange() {
    this.UpdateApprovalGroup.name = this.CreateApprovalGroup.name
    this.UpdateApprovalGroup.groups =this.CreateApprovalGroup.users.map((item: { accountNumber: any; }) => item.accountNumber)
  }
  transferToTarget(data: any) {
    this.CreateApprovalGroup.users=data.targetOption.map((item: { name: any; value: any; accountNumber: any; }) => ({
      name: item.name,
      accountNumber: item.value || item.accountNumber
    }));;
    this.UpdateApprovalGroup.name = this.CreateApprovalGroup.name;
    this.UpdateApprovalGroup.groups =this.CreateApprovalGroup.users.map((item: { accountNumber: any; }) => item.accountNumber);
  }
  transferToSource(data: any) {
    this.CreateApprovalGroup.users=data.targetOption.map((item: { name: any; value: any; accountNumber: any; }) => ({
      name: item.name,
      accountNumber: item.value || item.accountNumber
    }));;
    this.UpdateApprovalGroup.name = this.CreateApprovalGroup.name;
    this.UpdateApprovalGroup.groups =this.CreateApprovalGroup.users.map((item: { accountNumber: any; }) => item.accountNumber);
  }
  onChange(event: any) {
    this.disabled = event;
  }
}
